//
//  UIBarButtonItem+Extension.swift
//  WeiBo14
//
//  Created by rzj on 16/3/10.
//  Copyright © 2016年 rzj. All rights reserved.
//

import UIKit


//UIBarButtonItem分类
extension UIBarButtonItem {
    /** 快速创建自定义UIBarButtonItem */  //imgName/title为可选值 默认为nil
    convenience init(imgName:String? = nil, title:String? = nil, target:AnyObject?, action:Selector) {
        
        self.init()
        
        let btn = UIButton()
        
        //如果有图片设置图片
        if let name  = imgName {
            btn.setImage(UIImage(named: name), forState: .Normal)
            btn.setImage(UIImage(named: "\(name)_highlighted"), forState: .Highlighted)
        }
    
        //如果title有值 设置文字
        if let t = title {
            btn.setTitle(t, forState: .Normal)
            btn.setTitleColor(UIColor(colorLiteralRed: 52/255, green: 52/255, blue: 52/255, alpha: 1), forState: .Normal)
            btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        }
        
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        //属性设置完毕后注意最后设置按钮大小和customView
        btn.sizeToFit()
        customView = btn
    }
    
}

